#include "AddressBookDataAccess.h"

// 地址簿详情页显示  getAddressDetails
QList<Address> AddressBookDataAccess::getAddressBook(const QString &userId)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    QList<Address> addressBook;
    QString sql;
    sql = "SELECT Address_ID, Label, Name, Phone, Address FROM AddressBook WHERE user_id = :userId";
    query.prepare(sql);
    query.bindValue(":userId", userId);

    if (!query.exec())
    {
        qDebug() << "查询地址簿失败：" << query.lastError().text();
        return addressBook; // 返回空列表
    }

    while (query.next())
    {
        Address address;
        address.addressId = query.value("Address_ID").toInt();
        address.label = query.value("Label").toString();
        address.name = query.value("Name").toString();
        address.phone = query.value("Phone").toString();
        address.address = query.value("Address").toString();
        addressBook.append(address);
    }

    return addressBook;
}

// 添加地址簿信息
bool AddressBookDataAccess::addAddress(const int &userId, const QString &label, const QString &name, const QString &phone, const QString &address)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    // 准备 SQL 插入语句
    QString sql = "INSERT INTO AddressBook (user_Id, Label, Name, Phone, Address) VALUES (?, ?, ?, ?, ?)";
    query.prepare(sql);

    // 绑定参数
    query.addBindValue(userId); // 绑定 userId
    query.addBindValue(label);
    query.addBindValue(name);
    query.addBindValue(phone);
    query.addBindValue(address);

    // 执行 SQL 语句
    if (!query.exec())
    {
        qDebug() << "插入地址信息失败：" << query.lastError().text();
        return false;
    }

    return true;
}

// 修改地址簿信息
bool AddressBookDataAccess::editAddress(const int &addressId, const QString &label, const QString &name, const QString &phone, const QString &address)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    // 准备 SQL 更新语句
    QString sql = "UPDATE AddressBook SET Label = ?, Name = ?, Phone = ?, Address = ? WHERE Address_ID = ?";
    query.prepare(sql);

    // 绑定参数
    query.addBindValue(label);
    query.addBindValue(name);
    query.addBindValue(phone);
    query.addBindValue(address);
    query.addBindValue(addressId);

    // 执行 SQL 语句
    if (!query.exec())
    {
        qDebug() << "更新地址信息失败：" << query.lastError().text();
        return false;
    }

    return true;
}

// 删除地址簿信息
bool AddressBookDataAccess::deleteAddress(const int &addressId)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    // 准备 SQL 删除语句
    QString sql = "DELETE FROM AddressBook WHERE Address_ID = ?";
    query.prepare(sql);

    // 绑定参数
    query.addBindValue(addressId);

    // 执行 SQL 语句
    if (!query.exec())
    {
        qDebug() << "删除地址信息失败：" << query.lastError().text();
        return false;
    }

    return true;
}

Address AddressBookDataAccess::getAddressById(const int &addressId)
{
    DbConnector dbConnector;
    QSqlQuery query(dbConnector.getDatabase());

    Address address;
    QString sql;
    sql = "SELECT Name, Phone, Address FROM AddressBook WHERE address_id = :addressId";
    query.prepare(sql);
    query.bindValue(":addressId", addressId);

    if (!query.exec())
    {
        qDebug() << "根据ID查询地址失败：" << query.lastError().text();
        return address; // 返回空地址
    }

    while (query.next())
    {
        address.name = query.value("Name").toString();
        address.phone = query.value("Phone").toString();
        address.address = query.value("Address").toString();
    }

    return address;
}
